package personal.lijun.skeleton.springcloud.serviceprovider.service.controller;

import org.springframework.web.bind.annotation.*;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.CalendarUser;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.Role;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.service.CalendarUsersService;
import personal.lijun.skeleton.springcloud.common.data.ApiResultPrimitiveValue;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: lijun@sstcsoft.com
 * @Date: 2019-10-12 14:38
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private CalendarUsersService calendarUsersService;
    /**
     * 保存用户
     *
     * @param record 用户信息
     */
    @RequestMapping(value = "save", method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public CalendarUser save(@RequestBody CalendarUser record){
        return calendarUsersService.save(record);
    }

    /**
     * 增加一个新用户信息
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "addNew", method = {RequestMethod.POST})
    public CalendarUser addNewCalendarUsers(@RequestBody CalendarUser record){
        return calendarUsersService.addNewCalendarUsers(record);
    }

    /**
     * 更新指定用户信息
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public CalendarUser updateCalendarUsers(@RequestBody CalendarUser record){
        return calendarUsersService.updateCalendarUsers(record);
    }

    /**
     * 根据id删除指定用户
     *
     * @param id 用户id
     * @return
     */
    @RequestMapping(value = "deleteUsersById", method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public ApiResultPrimitiveValue<Integer> deleteCalendarUsersById(Long id){
        Integer delRow = calendarUsersService.deleteCalendarUsersById(id);
        return new ApiResultPrimitiveValue<>(delRow);
    }

    /**
     * 查询指定id的用户信息
     *
     * @param id 用户id
     * @return
     */
    @RequestMapping(value = "getUser", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public CalendarUser getCalendarUsersById(Long id){
        return calendarUsersService.getCalendarUsersById(id);
    }

    @RequestMapping(value = "findUserByEmail", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public CalendarUser findUserByEmail(String email){
        return calendarUsersService.findUserByEmail(email);
    }

    @RequestMapping(value = "findUsersByPartialEmail", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public List<CalendarUser> findUsersByPartialEmail(String email){
        return calendarUsersService.findUsersByPartialEmail(email);
    }

    /**
     * 根据用户id，查询此用户的所属角色
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping(value = "findUserRoles", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public List<Role> findUserRoles(Long userId){
        return calendarUsersService.findUserRoles(userId);
    }

    @GetMapping("")
    public List<CalendarUser> getUserList(@RequestParam(required = false) String name, @RequestParam(required = false) Boolean locked,
                                                        @RequestParam(required = false) String sort, int offset, int limit){
        return  calendarUsersService.getUserList(name,locked,sort,offset,limit);
    }
}
